package swing;

import jdbc_code.jdbcDemo7;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;

/**
 * 登录界面
 */
public class Login extends JFrame {

    public Login() {
        //取消布局管理器设置
        this.setLayout(null);

        //创建标签
        JLabel username = new JLabel("用户名：");
        username.setBounds(160,180,100,50);
        JLabel pwd = new JLabel("密码：");
        pwd.setBounds(172,240,100,50);

        //创建文本框
        JTextField jd1 = new JTextField();
        jd1.setBounds(220,190,200,30);
        //创建密码框
        JPasswordField jpd = new JPasswordField();
        jpd.setEchoChar('*'); //内容以"*"显示
        jpd.setBounds(220,250,200,30);

        //创建按钮
        JButton btn1 = new JButton("登录");
        btn1.setBounds(100,350,100,50);
        JButton btn2 = new JButton("注册");
        btn2.setBounds(240,350,100,50);
        JButton btn3 = new JButton("重置");
        btn3.setBounds(380,350,100,50);


        this.setTitle("登录界面");
        this.setSize(600,600);
        this.setVisible(true);
        //获取窗体容器
        this.setLocationRelativeTo(null);
        Container contentPane = this.getContentPane();
        contentPane.add(username);
        contentPane.add(pwd);
        contentPane.add(btn1);
        contentPane.add(btn2);
        contentPane.add(btn3);
        contentPane.add(jd1);
        contentPane.add(jpd);

        //给重置按钮添加监听事件
        btn3.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                jd1.setText("");  //设置文本框为空
                jd1.requestFocus(); //获得焦点
                jpd.setText("");  //设置密码框为空
            }
        });

        //给登录按钮添加监听事件
        btn1.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //获取账号文本框的值
                String username = jd1.getText();
                //获取密码框的值
                String password = jpd.getText();

                boolean flag = new jdbcDemo7().login2(username, password);

                if (flag) {
                    //登录成功
                    new LoginSuccessful(Login.this).setVisible(true);
                } else {
                    //登录失败
                    //System.out.println("用户名或密码不正确!");
                    new LoginFailure(Login.this).setVisible(true);
                }
            }
        });

        //给注册按钮添加监听事件
        btn2.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new Registered(Login.this).setVisible(true);
            }
        });

    }

    public static void main(String[] args) {
        new Login();
    }

}
